import java.util.ArrayList;
import java.util.List;

public class Permutations {
    //定义返回的二维列表
    public List<List<Integer>> permute(int[] nums){
        List<List<Integer>> result=new ArrayList<>();
        backtrack(nums,new ArrayList<>(),result);
        return result;
    }
    private void backtrack(int[] nums,List<Integer>temp,List<List<Integer>> result){
        if(temp.size()==nums.length){
            result.add(new ArrayList<>(temp));
        }else{
            for(int i=0;i<nums.length;i++){
                if(temp.contains(nums[i])){
                    //如果是已经遍历过了
                    continue;//跳过当前的判断的元素
                }
                temp.add(nums[i]);
                backtrack(nums,temp,result);
                temp.remove(temp.size()-1); //移动这个下标的元素
            }
        }
    }

    public static void main(String[] args) {
        Permutations permutations = new Permutations();
        int[] nums1={1,2,3};
        int[] nums2={1,2,3,4,0};
        int[] nums3={1,2,3,5};
        System.out.println(permutations.permute(nums1));
        System.out.println();
        System.out.println(permutations.permute(nums2));
        System.out.println();
        System.out.println(permutations.permute(nums3));

    }
}
